<?php
class Admin_Model_Accounts extends Zend_Db_Table_Abstract {

    /**
     * Model Account
     * @author Nguyen Manh Hung
     */
    protected $_name = TABLE_USERS;
    protected $_id = 'id';

    /**
     * update user
     * @author Nguyen Manh Hung	
     */
    public function updateUser($id, $username, $password, $email, $gender = 1, $active = 1, $group = 2, $dob = '') {
        $row = $this->find($id)->current();

        if ($row) {
            if ($username)
                $row->username = $username;

            if ($password != '') {
                $password = md5($password);
                $row->password = $password;
            }

            if ($email)
                $row->email = $email;            
            $row->gender = $gender;

            if ($dob != '') {
                $row->dob = $dob;
            }
            
            $row->active = $active;
            $row->group = $group;
            $row->save();
            return 1;
        } else {
            return 0;
        }
    }

    /**
     *
     * @author Nguyen Manh Hung
     * @param int $id        	
     *
     *
     */
    public function deleteUser($id) {
        $rowUser = $this->find($id)->current();

        if (!$rowUser) {
            return 0;
        } else {
            $rowUser->delete();
            return 1;
        }
    }

    /**
     * Fetch user
     * @author Nguyen Manh Hung
     * @param Array $filters  
     * @param String $sortFeild 
     * @param int $limit 
     * @param int $page  
     */
    public function fetchUser($filters = array(), $sortFeild = null, $islimit = 0, $page = 1) {
        $select = $this->select();
        // add cac filter vao truy van tim kiem
        if (count($filters) > 0) {
            foreach ($filters as $feild => $filter) {
                $select->where($feild . ' =?', $filter);
            }
        }

        // add sort vao truy van
        if (null != $sortFeild) {
            $select->order($sortFeild);
        }

        // add limit - offset vao truy van
        if (!$islimit) {            
            $select->limitPage($page, NUM_PER_PAGE);
        }
        return $this->fetchAll($select);
    }

}

